#!/user/bin/env python
# coding:utf-8
# 面向过程变成,虫子吃蚂蚁

# import random
# ant_point = random.randint(0,20)
# worm_point = random.randint(0,20)
#
# print "ant:"+str(ant_point)+" worm:"+str(worm_point)
#
# step = [-2,-3,2,3]
#
# while True:
#     astep = random.choice(step)
#     if 0 <= ant_point+astep <=20:
#         ant_point += astep
#
#     astep = random.choice(step)
#     if 0 <= worm_point+astep <=20:
#         worm_point += astep
#
#     print "ant:"+str(ant_point)+" worm:"+str(worm_point)
#
#     if str(ant_point) == str(worm_point):
#         print "eat"
#         break

# 面向对象,虫子吃蚂蚁
import random


# 定义父类
class Sprite:
    step = [-2, -3, 2, 3]

    def __init__(self, gm, point=None):
        self.gm = gm
        if point is None:
            self.point = random.randint(0, 20)
        else:
            self.point = point

    def jump(self):
        asteep = random.choice(Sprite.step)
        if 0 <= self.point + asteep <= 20:
            self.point += asteep


# 定义蚂蚁
class Ant(Sprite):
    def __init__(self, gm, point=None):
        super.__init__(gm, point)
        self.gm.set_point("ant", self.point)

    def jump(self):
        super.jump()
        self.gm.set_point("ant", self.point)


# 定义虫子
class Worm(Sprite):
    def __init__(self, gm, point=None):
        super.__init__(gm, point)
        self.gm.set_point("worm", self.point)

    def jump(self):
        super.jump()
        self.gm.set_point("worm", self.point)


# 定义地图类
class Map:
    def __init__(self):
        self.ant_point = None
        self.worm_point = None

    def catchd(self):
        print("eat")
        if self.ant_point is not None and self.worm_point is not None and self.ant_point == self.worm_point:
            return True

    def set_point(self, src, point):
        if src == "ant":
            self.ant_point = point
        if src == "worm":
            self.worm_point = point


if __name__ == "__main__":
    Gm = Map()
    Worm = Worm(Gm)
    Ant = Ant(Gm)
    while not Gm.catchd():
        Worm.jump()
        Ant.jump()
